{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geetools import tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipygee import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_image = ee.Image('LANDSAT/LT05/C01/T1_SR/LT05_226087_20000102')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `get_date_band`\n",
    "Get the date of an image, compute how many `units` (for example `day`) has ellpsed since the epoch (1970-01-01) and set it to a band (called `date`) and a property (called `unit_since_epoch`, for example, `day_since_epoch`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "date_band = tools.date.getDateBand(test_image, 'day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6ec1b6129db24743ad6aa3e2b39f5095",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Accordion(children=(Output(),), _titles={'0': 'Loading...'}),))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eprint(date_band)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `date_since_epoch`\n",
    "Given an ellapsed time since epoch (for example the result of `get_date_band`) compute what day it is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_date = date_band.get('day_since_epoch')\n",
    "date_since_epoch = tools.date.dateSinceEpoch(image_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5cec3f901846429e91022d4294ae949f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Accordion(children=(Output(),), _titles={'0': 'Loading...'}),))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-5:\n",
      "Traceback (most recent call last):\n",
      "  File \"/usr/lib/python3.5/threading.py\", line 914, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/usr/lib/python3.5/threading.py\", line 862, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/labgeo1/Python/ipygee/ipygee/eprint.py\", line 56, in get_info\n",
      "    eewidget = dispatcher.dispatch(eeobject)\n",
      "  File \"/home/labgeo1/Python/ipygee/ipygee/dispatcher.py\", line 41, in dispatch\n",
      "    widget = dispatchDate(info)\n",
      "  File \"/home/labgeo1/Python/ipygee/ipygee/dispatcher.py\", line 128, in dispatchDate\n",
      "    return Label(info)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/ipywidgets/widgets/widget_string.py\", line 31, in __init__\n",
      "    super(_String, self).__init__(**kwargs)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/ipywidgets/widgets/widget.py\", line 411, in __init__\n",
      "    super(Widget, self).__init__(**kwargs)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/traitlets/traitlets.py\", line 997, in __init__\n",
      "    setattr(self, key, value)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/traitlets/traitlets.py\", line 585, in __set__\n",
      "    self.set(obj, value)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/traitlets/traitlets.py\", line 559, in set\n",
      "    new_value = self._validate(obj, value)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/traitlets/traitlets.py\", line 591, in _validate\n",
      "    value = self.validate(obj, value)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/traitlets/traitlets.py\", line 2054, in validate\n",
      "    self.error(obj, value)\n",
      "  File \"/home/labgeo1/Python/envs/geepy3/lib/python3.5/site-packages/traitlets/traitlets.py\", line 625, in error\n",
      "    raise TraitError(e)\n",
      "traitlets.traitlets.TraitError: The 'value' trait of a Label instance must be a unicode string, but a value of {'type': 'Date', 'value': 946771200000} <class 'dict'> was specified.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "eprint(date_since_epoch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `unit_since_epoch`\n",
    "Return the number of `unit` (for example, `day`) since the epoch (1970-1-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = ee.Date('2000-01-02')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "days = tools.date.unitSinceEpoch(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3387943de1a64fafb9a4a0a516480a8d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Accordion(children=(Output(),), _titles={'0': 'Loading...'}),))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eprint(days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
